/**
 * Created by leiweibo on 7/5/15.
 */

var app = angular.module('patientservice', []);

app.factory('PatientService', function($http, $window, BaseService) {
  return {
    create:  function(data, success, error) {
      $http.post(BaseService.baseUrl + '/patient/create', data).success(success).error(error);
    },
    update: function(data, success, error) {
      $http.put(BaseService.baseUrl + '/patient/update', data).success(success).error(error);
    },
    delete: function(data, success, error) {
      $http.delete(BaseService.baseUrl + '/patient/delete?id=' + data.id).success(success).error(error);
    },
    list: function( office, doctor, success) {
      var queryParam = "?";
      if (office) {
        queryParam += ('office=' + office)
      }

      if (doctor) {
        if (queryParam.length > 1) {
          queryParam += ('&doctor=' + doctor)
        } else {
          queryParam += ('doctor=' + doctor)
        }
      }

      $http.get(BaseService.baseUrl + '/patient/query' + queryParam).success(success);
    },
    list_by_doctor: function(pageNo, pageSize, doctorId, success) {
      var queryStr = '';
      if (doctorId) {
        queryStr = '&doctorId=' + doctorId;
      }
      $http.get(BaseService.baseUrl + "/patient/list?pageNo=" + pageNo + "&pageSize=" + pageSize + queryStr).success(success);
    },
    list_by_hospital: function(pageNo, pageSize, provinceId, hospitalId, diseaseId, start_time, end_time, success) {
      var queryStr = '';
      if (hospitalId) {
        queryStr += '&hospitalId=' + hospitalId;
      }
      if (provinceId) {
        queryStr += '&provinceId=' + provinceId;
      }

      if (diseaseId) {
        queryStr += '&diseaseId=' + diseaseId;
      }

      if (start_time) {
        queryStr += '&start=' + start_time;
      }

      if (end_time) {
        queryStr += '&end=' + end_time;
      }

      $http.get(BaseService.baseUrl + "/patient/list?pageNo=" + pageNo + "&pageSize=" + pageSize + queryStr).success(success);
    },
    get_patint_by_id: function(patientId, success) {
      $http.get(BaseService.baseUrl + '/patient?patientId=' + patientId).success(success);
    },
    export_patient: function(provinceId, hospitalId, diseaseId, start_time, end_time, success) {
      //$window.location = BaseService.baseUrl + '/patient/excel?uid=123';
      //$http.get(BaseService.baseUrl + '/patient/excel')

      var queryStr = '';
      if (hospitalId) {
        queryStr += '&hospitalId=' + hospitalId;
      }
      if (provinceId) {
        queryStr += '&provinceId=' + provinceId;
      }

      if (diseaseId) {
        queryStr += '&diseaseId=' + diseaseId;
      }

      if (start_time) {
        queryStr += '&start=' + start_time;
      }

      if (end_time) {
        queryStr += '&end=' + end_time;
      }
      $http.get(BaseService.baseUrl + '/patient/excel?tmp=true&'+queryStr, {responseType: "arraybuffer" }).success(success)
    }

  }
}).factory('PatientData', function() {
  var savedData = {};
  function set(data) {
    savedData = data;
  }

  function get() {
    return savedData
  }

  return {
    get: get,
    set: set
  }
});
